/*
最大流：BFS 增广路径法 O(n*m*m)
存边：正向和反向都需要存储，及时初始反向边全都为0
链式前向星 存图
bfs()判断是否迭代，并记录要更新的值
update()更新边权
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=202;
const int M=10005;

int n,m,s,t;
int flag[N][N];
struct edge{
    int to,next;
    LL w;
};
edge edges[M];
int head[N];
int cnt=1;
LL ans=0;

void adde(int u,int v,int w){
    edges[cnt].to=v;
    edges[cnt].w=w;
    edges[cnt].next=head[u];
    head[u]=cnt;
    cnt++;
}

LL wmin;
bool visit[N];
int pre[N];
bool bfs(){
    queue<int> que;
    que.push(s);
    memset(visit,0,sizeof(visit));
    visit[s]=true;
    wmin=LLONG_MAX;
    while(!que.empty()){
        int size=que.size();
        for(int i=0;i<size;i++){
            int p=que.front();
            que.pop();
            for(int i=head[p];i!=0;i=edges[i].next)
            {
                int t1=edges[i].to;
                LL w=edges[i].w;
                if(w==0) continue;
                if(visit[t1]) continue;
                que.push(t1);
                pre[t1]=i;
                visit[t1]=true;
                wmin=min(wmin,w);
                if(t==t1) return true;
            }
        }
    }
    return false;
}
void update(){
    ans+=wmin;
    int x=t;
    while(x!=s)
    {
        int pos=pre[x];
        int neg;
        if(pos%2==1) neg=pos+1;
        else neg=pos-1;
        edges[pos].w-=wmin;
        edges[neg].w+=wmin;
        x=edges[neg].to;
    }
}

int main(){
    scanf("%d%d%d%d",&n,&m,&s,&t);
    memset(flag,0,sizeof(flag));
    for(int i=0;i<m;i++){
        int s1,t1;
        LL w1;
        scanf("%d%d%lld",&s1,&t1,&w1);
        if(flag[s1][t1]==0){
            flag[s1][t1]=cnt;
            flag[t1][s1]=cnt+1;
            adde(s1,t1,w1);
            adde(t1,s1,0);
        }
        else{
            edges[flag[s1][t1]].w+=w1;
        }
    }
    while(bfs()){
        update();
    }
    printf("%lld",ans);
    return 0;
}